package queue;

import java.util.LinkedList;

/**
 * @description: 1047. 删除字符串中的所有相邻重复项
 * https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/description/
 * @author: caoyihong
 * @date: 2025-10-11 13:56
 * @Version 1.0
 **/
public class RemoveDuplicates {
    public static void main(String[] args) {
        String s = "azxxzy";
        String result = removeDuplicates(s);
        System.out.println(result);
    }
    public static String removeDuplicates(String s) {
        LinkedList<Character> list = new LinkedList<>();
        char[] chars = s.toCharArray();
        for (char c : chars) {
            if (list.isEmpty()) {
                list.addFirst(c);
                continue;
            }
            Character first = list.peekLast();
            if (first == c) {
                list.removeLast();
            } else {
                list.add(c);
            }
        }
        if (list.isEmpty()) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            for (char c : list) {
                sb.append(c);
            }
            return sb.toString();
        }
    }
}
